<template>
  <div class="app-container">
    <el-button type="primary" @click="create">创建煤炭数据抓取定时任务</el-button>
    <el-button type="warning" @click="stop">停止煤炭数据抓取定时任务</el-button>
    <el-button type="success" @click="start">启动煤炭数据抓取定时任务</el-button>
    <el-button type="danger" @click="delTask">删除煤炭数据抓取定时任务</el-button>

    <el-button type="warning" @click="manual">手动触发所有数据抓取</el-button>
    <el-button type="info" @click="getTaskStatus">获取调度器状态</el-button>
    <el-button type="success" @click="getDataStatus">获取数据抓取状态</el-button>
  </div>
</template>

<script setup>
import MonitorAPI from '@/api/monitor/monitor.api';

// 创建并启动煤炭数据抓取定时任务，每天凌晨1点执行
const create = useDebounceFn(() => {
  MonitorAPI.createTask()
    .then((data) => {
      ElMessage({
        message: '创建成功',
        type: 'success',
      });
    })
    .catch((err) => {});
}, 300);

const stop = useDebounceFn(() => {
  MonitorAPI.stopTask()
    .then((data) => {
      ElMessage({
        message: data || '停止成功',
        type: 'error',
      });
    })
    .catch((err) => {
      ElMessage({
        message: '停止失败',
        type: 'error',
      });
    });
}, 300);

const start = useDebounceFn(() => {
  MonitorAPI.startTask()
    .then((data) => {
      ElMessage({
        message: data || '启动成功',
        type: 'success',
      });
    })
    .catch((err) => {
      ElMessage({
        message: '启动失败',
        type: 'error',
      });
    });
}, 300);

const delTask = useDebounceFn(() => {
    ElMessageBox.confirm('确认删除?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      MonitorAPI.deleteTask().then((data) => {
        ElMessage({
          message: data || '删除成功',
          type: 'success',
        });
      })
      .catch((err) => {});
    })
    .catch(() => {});
}, 300);

const manual = useDebounceFn(() => {
  MonitorAPI.manualAllFetch()
    .then((data) => {
      ElMessage({
        message: '手动触发成功',
        type: 'success',
      });
    })
    .catch((err) => {
      ElMessage({
        message: '手动触发失败',
        type: 'error',
      });
    });
}, 300);

const getTaskStatus = useDebounceFn(() => {
  MonitorAPI.getTaskStatus()
    .then((data) => {
      ElMessage({
        message: h('p', { style: 'line-height: 1; font-size: 14px' }, [
          h('span', { style: 'color: red' }, `调度器状态：${data.coalDataJobStatusDesc}`),
          h('br'),
          h('br'),
          h('span', { style: 'color: #595656' }, `调度器名称：${data.coalDataJobRemark}`),
        ]),
        type: 'success',
        duration: 5000,
      });
    })
    .catch((err) => {
      ElMessage({
        message: '获取调度器状态',
        type: 'error',
      });
    });
}, 300);

const getDataStatus = useDebounceFn(() => {
  MonitorAPI.getDataStatus()
    .then((data) => {
      ElMessage({
        message: h('p', { style: 'line-height: 1; font-size: 14px' }, [
          h('span', { style: 'color: #595656' }, '  OCFI数据抓取：'),
          h('span', { style: 'color: red' }, `${data.ocfiDataCount}`),
          h('br'),
          h('br'),
          h('span', { style: 'color: #595656' }, 'BSSPI数据抓取：'),
          h('span', { style: 'color: red' }, `${data.bsspiDataCount}`),
          h('br'),
          h('br'),
          h('span', { style: 'color: #595656' }, '  BSPI数据抓取：'),
          h('span', { style: 'color: red' }, `${data.bspiDataCount}`),
        ]),
        type: 'success',
        duration: 5000,
      });
    })
    .catch((err) => {
      ElMessage({
        message: '获取数据抓取状态',
        type: 'error',
      });
    });
}, 300);
</script>

<style lang="scss" scoped>
</style>